<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 前台会员消息ajax类
 * @package  ajax
 * @subpackage  shop
 * @author   allen.qiang
 */

quanli_require('module/base/customer/custMessage.class.php');

class ajax_message {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询会员消息
                    $this->searchMessage();
                    break;

                case 2: //回复消息
                    $this->replyMessage();
                    break;

                case 3: //查询未读会员消息数量
                    $this->noReadMessage();
                    break;

                case 4:  //删除会员消息
                    $this->deleteMessage();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;


            }

        } else {
            returnJsonData('参数错误');
        }

    }

    /**
     * 查询会员消息
     */
    private function searchMessage() {

        global $G_SHOP;

        //检查是否登录
        if (!$G_SHOP->currentPCustID) {
            returnJsonData('请先登录', 404);
        }

        if (!isset($_REQUEST['page']) || !quanli_is_int($_REQUEST['page'])) {
            $_REQUEST['page'] = 1;
        }

        if (!isset($_REQUEST['is_reply']) || !in_array($_REQUEST['is_reply'], array( '0', '1' ))) {
            $_REQUEST['is_reply'] = -1;
        }

        if (!isset($_REQUEST['is_read']) || !in_array($_REQUEST['is_read'], array( '0', '1' ))) {
            $_REQUEST['is_read'] = -1;
        }

        //实例会员消息类
        $custMessage = new custMessage();

        //查询订单信息
        $args_array = array(
            'pcustID' => $G_SHOP->currentPCustID,
            'page' => $_REQUEST['page'],
            'is_read' => $_REQUEST['is_read'],
            'is_reply' => $_REQUEST['is_reply'],
            'rp' =>5,
            'sortname' => 'message_id',
            'sortorder' => 'DESC'
        );

        $result_array['message'] = $custMessage->searchCustMessage( $args_array );
        if ($result_array['message']) {

            $result_array['paging'] = shop_page($args_array['page'], $args_array['count'], $args_array['rp'], 'javascript: G.message.search(%d);', $G_SHOP->smarty);

            returnJsonData('ok', 200, $result_array);

        } else {
            returnJsonData('会员消息没有找到', 404);
        }

    }

    /**
     * 回复消息
     */
    private function replyMessage() {

        global $G_SHOP;

        //检查是否登录
        if (!$G_SHOP->currentPCustID) {
            returnJsonData('请先登录', 404);
        }

        if (!isset($_REQUEST['message_id']) || !quanli_is_int($_REQUEST['message_id'])) {
            returnJsonData('缺少参数', 404);
        }

        if (!isset($_REQUEST['reply_content']) || empty($_REQUEST['reply_content']) || strlen($_REQUEST['reply_content']) > 450) {
            returnJsonData('请填写回复内容且最多填写150个汉字', 404);
        }

        //实例会员消息类
        $custMessage = new custMessage();

        //回复消息
        $result_array = $custMessage->replyMessage( $G_SHOP->currentPCustID, $_REQUEST['message_id'], $_REQUEST['reply_content'] );

        returnJsonData($result_array['message'], $result_array['code']);

    }

    /**
     * 会员未读消息数量、总消息数量
     */
    private function noReadMessage(){

        global $G_SHOP;

        //检查是否登录
        if (!$G_SHOP->currentPCustID) {
            returnJsonData('请先登录', 404);
        }

        $args_array = array(
            'pcustID' => $G_SHOP->currentPCustID,
        );

        $count_array = array(
            'all_count' => 0,
            'no_read_count' => 0
        );

        //实例会员消息类
        $custMessage = new custMessage();

        $message_array = $custMessage->searchCustMessage( $args_array );

        if($message_array){
            foreach($message_array as $msg){

                if($msg['is_read'] == 0)
                    $count_array['no_read_count']++;

                $count_array['all_count']++;

            }
        }

        returnJsonData('ok', 200, $count_array);

    }

    /*
     * 删除会员消息
     */
    public function deleteMessage(){

        global $G_SHOP;

        //检查是否登录
        if (!$G_SHOP->currentPCustID) {
            returnJsonData('请先登录', 404);
        }

        if( !isset( $_REQUEST['uuid'] ) ||  $_REQUEST['uuid'] ==''){
            returnJsonData('参数错误',404);
        }

        $args_array = array(
            'uuid'=>$_REQUEST['uuid']
        );
        //实例会员消息类
        $custMessage = new custMessage();
        $result_array = $custMessage->deleteMessage( $args_array );

        returnJsonData($result_array['message'], $result_array['code']);
    }

}